<?php

/**
 * @author Alexandr McArrow
 * @copyright 2008
 * @project Board Site
 */

function forming_capcha()
{
	global $BS;
	global $_SESSION;
	
		$CAP = array();
		@unlink($BS['ROOT']."cap/".$_SESSION['captcha'].".png");
		
        
        $CAP = captcha_get_new();
		$_SESSION['capdata'] = $CAP[1];        
        $_SESSION['captcha'] = uniqid("cap_",false);
        
        imagepng($CAP[0],$BS['ROOT']."cap/".$_SESSION['captcha'].".png");

        
	
	$BS['DEBUG'][]="FORMING CAPCHA (".$_SESSION['captcha'].") [".$CAP[1]."]";
	
}

function captcha_get_new()
{
    global $BS;

    $w = 200;
    $h = 60;

    $pic = imagecreatetruecolor($w,$h);
    $col = $BS['CAPTCHACHARCOUNT'];
    $stringa = "ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ";
    $string = "01234567890123456789";
    $strcount = strlen($string) - 1;
    $strcounta = strlen($stringa) - 1;

    /// fonts

    $Fonts = array();

    $handle = opendir($BS['ROOT'].'Fonts/');

    while($file = readdir($handle))
    {
        if($file == "." || $file == ".." || $file[0] == ".")
        {

        } else
        {
            $Fonts[] = $BS['ROOT'].'Fonts/'.$file;
        }
    }
    closedir($handle);

    /// fonts
    $back = 255;
    $back2 = 254;
    $back3 = 20;
    $bgc = imagecolorallocate($pic,$back,$back,$back);
    $bgc2 = imagecolorallocate($pic,$back2,$back2,$back2);
    $bgc3 = imagecolorallocate($pic,$back3,$back3,$back3);
	$alpha = imagecolortransparent($pic,$bgc);
	
	imagefilledrectangle($pic,0,0,$w,$h,$alpha);
	
	
	$data="";
	
    for($i = 0; $i < $col; $i++)
    {
        $char = $string[rand(0,$strcount)];
        $chara = $stringa[rand(0,$strcounta)];
        $data .= $char;
        
		$color = imagecolorallocate($pic,0,0,0);
        imagefttext($pic,30,0,15 + ($i * 30),28,$bgc2,$BS['ROOT'].'Fonts/calibri.ttf',$char);
        imagefttext($pic,30,0,10 + ($i * 32),58,$bgc3,$BS['ROOT'].'Fonts/calibri.ttf',$chara);
                
    }
	
	$blank = imagecreatefrompng($BS['ROOT']."blank.png");	
	$alpha = imagecolortransparent($blank,$bgc);
	
	$pic2 = imagecreatetruecolor($w,$h);
	imagefilledrectangle($pic2,0,0,$w,$h,$bgc);
		
	imagecopymerge($blank,$pic2,0,0,0,0,$w,$h,99);
		
	imagecopymerge($pic,$blank,0,0,0,0,$w,$h,30);
	
	$blank = imagecreatefrompng($BS['ROOT']."blank.png");	
	$alpha = imagecolortransparent($blank,$bgc);
	
	imagecopymerge($pic,$blank,0,0,0,0,$w,$h,50);

    return array($pic,$data);
    imagedestroy($pic);
}


?>